/**
 *
 * @author poemdexter, updated to hey0 v117 by Eric Hushagen
 */

import java.util.List;
import java.util.logging.Logger;

public class poemLoggerListener extends PluginListener {

    protected static final Logger log = Logger.getLogger("Minecraft");

    public boolean onCommand(Player player, String[] split) {
        if (!player.canUseCommand(split[0])) {
            return false;
        }

        if (split[0].equalsIgnoreCase("/respond")) {
            if (split.length != 4) {
                player.sendMessage("Correct use is: /respond [x][y][z]");
                return true;
            }

            try {
                Integer.parseInt(split[1]);
                Integer.parseInt(split[2]);
                Integer.parseInt(split[3]);
            } catch (NumberFormatException ex) {
                player.sendMessage(Colors.Rose + "Not using valid numbers.");
                return true;
            }

            Location location = new Location();
            location.x = Integer.parseInt(split[1]);
            location.y = Integer.parseInt(split[2])+1;
            location.z = Integer.parseInt(split[3]);
            location.rotX = player.getRotation();
            location.rotY = player.getPitch();

            player.teleportTo(location);
            player.sendMessage("To the rescue!");
            return true;
        }
        return false;
    }

    public boolean onBlockCreate(Player player, Block blockPlaced, Block blockClicked, int itemInHand) {

        if (itemInHand == 7 || itemInHand == 49 || itemInHand == 8 || itemInHand == 9 || itemInHand == 10 || itemInHand == 11 || itemInHand == 259
                || itemInHand == 51 || itemInHand == 326 || itemInHand == 46 || itemInHand == 327) {
            String loc = (int)player.getX() + " " + (int)player.getY() + " " + (int)player.getZ();
            String message = "EMERGENCY: " + player.getName() + " has used " + getName(itemInHand) + " " + loc;
            tellServer(message);
            tellMods(message);
            return false;
        }
        return false;
    }

    public String getName(int x) {
        if (x == 8) {
            return "water spawner.";
        } else if (x == 9) {
            return "water spawner.";
        } else if (x == 10) {
            return "lava spawner.";
        } else if (x == 11) {
            return "lava spawner.";
        } else if (x == 259) {
            return "lighter.";
        } else if (x == 51) {
            return "fire spawner.";
        } else if (x == 326) {
            return "water bucket.";
        } else if (x == 46) {
            return "TNT.";
            } else if (x == 49) {
            return "obsidian.";
            } else if (x == 7) {
            return "bedrock.";
        } else if (x == 327) {
            return "lava bucket.";
        } else {
            return "";
        }
    }

    public void tellServer(String message) {
        log.info(message);
    }

    public void tellMods(String message) {
        List<Player> pList = etc.getInstance().getServer().getPlayerList();
        for (Player p : pList) {
            if (p.isInGroup("mods")) {
                p.sendMessage(Colors.Rose + message);
            }
            if (p.isInGroup("minimods")) {
                p.sendMessage(Colors.Rose + message);
            }
        }
    }

}
